package com.css.common.core.utils;

import cn.hutool.core.util.NumberUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName InvoiceUtil
 * @Auther qiumengmeng
 * @Date 2023/4/26 下午4:02
 */
public class InvoiceUtil {

    /**
     * 反算不含税单价
     *
     * @param hsje
     * @param slv
     * @param sl
     * @return
     */
    public static BigDecimal toBhsDj(BigDecimal hsje, BigDecimal slv, BigDecimal sl) {
        BigDecimal hsDj = hsje.divide(sl, 18, RoundingMode.HALF_UP);
        BigDecimal rateAdd1 = new BigDecimal(1).add(slv);
        return hsDj.divide(rateAdd1, 8, RoundingMode.HALF_UP);
    }

    /**
     * 反算含税单价
     *
     * @param hsje
     * @param sl
     * @return
     */
    public static BigDecimal toHsDj(BigDecimal hsje, BigDecimal sl) {
        return hsje.divide(sl, 2, RoundingMode.HALF_UP);
    }

    /**
     * 反算金额
     *
     * @param hsje
     * @param slv
     * @return
     */
    public static BigDecimal toJe(BigDecimal hsje, BigDecimal slv) {
        BigDecimal rateAdd1 = new BigDecimal(1).add(slv);
        return hsje.divide(rateAdd1, 2, RoundingMode.HALF_UP);
    }

    /**
     * 税额 -> 金额
     *
     * @param se
     * @param slv
     * @return
     */
    public static BigDecimal se2Je(BigDecimal se, BigDecimal slv) {
        return se.divide(slv, 2, RoundingMode.HALF_UP);
    }

    /**
     * 反算税额
     *
     * @param jshj
     * @param slv
     * @return
     */
    public static BigDecimal toSe(BigDecimal jshj, BigDecimal slv) {
        BigDecimal je = toJe(jshj, slv);
        return jshj.subtract(je).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 反算不含税单价
     *
     * @param hsdj
     * @param slv
     * @return
     */
    public static BigDecimal toBhsdj(BigDecimal hsdj, BigDecimal slv) {
        BigDecimal slvDecimal = (new BigDecimal("1")).add(slv);
        return hsdj.divide(slvDecimal, 13, RoundingMode.HALF_UP);
    }

    /**
     * 反算不含税金额
     *
     * @param hsje
     * @param slv
     * @return
     */
    public static BigDecimal toBhsje(BigDecimal hsje, BigDecimal slv) {
        if (BaseUtil.isNull(slv)) return hsje;
        BigDecimal slvDecimal = (new BigDecimal("1")).add(slv);
        return hsje.divide(slvDecimal, 2, RoundingMode.HALF_UP);
    }

    public static List<String> zzsslOrZsl(String zzsslzsl) {
        List<String> slvList = new ArrayList<>();
        if (StringUtils.isBlank(zzsslzsl)) {
            return slvList;
        }
        String[] zzsslzslArray = zzsslzsl.split("、");
        for (String str : zzsslzslArray) {
            String zzsslStrNumber = str.replace("%", "");
            if (NumberUtil.isNumber(zzsslStrNumber)) {
                BigDecimal zzsslDecimal = (new BigDecimal(zzsslStrNumber)).divide(BigDecimal.valueOf(100), 6, RoundingMode.HALF_UP);
                slvList.add(zzsslDecimal.stripTrailingZeros().toPlainString());
            }
        }
        return slvList.stream().distinct().collect(Collectors.toList());
    }

    public static List<String> zzsslZsl(String zzssl, String zsl) {
        List<String> kysl = new ArrayList<>();
        // 增值税税率
        kysl.addAll(zzsslOrZsl(zzssl));
        // 征收率
        kysl.addAll(zzsslOrZsl(zsl));
        return kysl.stream().distinct().collect(Collectors.toList());
    }

    public static BigDecimal stringToDecimal(String decimal) {
        if (StringUtils.isBlank(decimal)) {
            return null;
        }
        return new BigDecimal(decimal);
    }

    public static String decimalToString(BigDecimal decimal) {
        if (decimal == null) {
            return null;
        }
        return decimal.stripTrailingZeros().toPlainString();
    }

    public static String stringDecimalToString(String decimal) {
        if (StringUtils.isBlank(decimal)) {
            return null;
        }
        BigDecimal stringDecimal = new BigDecimal(decimal);
        return stringDecimal.stripTrailingZeros().toPlainString();
    }

    public static BigDecimal toHsJe(BigDecimal bhsje, BigDecimal slv) {
        BigDecimal slvDecimal = (new BigDecimal("1")).add(slv);
        return bhsje.multiply(slvDecimal).setScale(2, RoundingMode.HALF_UP);
    }
}
